﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SocialShare.BusinessEntities;
using Utilities;
using SocialShare.BLL;
using System.Globalization;

namespace SocialShare.BusinessLogicLayer
{
    internal partial class SocialShare_BlogsBLL
    {
        internal BlogPost GetSocialShareBlogByBlogId(int blogId)
        {
             var result =
                from
                    blogs in _socialShareDatabaseContext.SocialShare_Blogs
                join
                    profiles in _socialShareDatabaseContext.SocialShare_MemberProfiles
                 on
                    blogs.UserId equals profiles.UserId
                where
                   blogs.BlogId==blogId
                select new
                {
                   blogs.BlogId,
                   blogs.DateCreated,
                   blogs.IsCommentsAllowed,
                   blogs.PostContent,
                   blogs.PostStatus,
                   blogs.PostSummary,
                   blogs.PostTitle,
                   blogs.Tags,
                   blogs.UserId,
                   profiles.BlogName
                };

             return result.ToList().ConvertAll(x => new BlogPost
             {
                 BlogId = x.BlogId,
                 PostTitle = x.PostTitle,
                 PostSummary = x.PostSummary,
                 PostContent = x.PostContent,
                 DateCreated = x.DateCreated,
                 UserId = x.UserId,
                 IsCommentsAllowed = x.IsCommentsAllowed,
                 PostStatus = x.PostStatus.Value,
                 Tags = x.Tags,
                 BlogName = x.BlogName,
                 PostCleanUrl = Utilities.SiteSettings.BaseUrl+"blogs/"+x.BlogName + "/" + x.BlogId + "-" + Imgalib.Utility.StringFormatter.CreateCleanUrl(x.PostTitle)
             }).First();
        }

        internal List<BlogPost> GetAllSocialShareActiveBlogsPaged(Int32 startRowIndex = default(Int32), int maximumRows = default(Int32))
        {
            var result =
               from
                   blogs in _socialShareDatabaseContext.SocialShare_Blogs
               join
                   profiles in _socialShareDatabaseContext.SocialShare_MemberProfiles
                on
                   blogs.UserId equals profiles.UserId
               where
                  blogs.PostStatus ==(Int32)BlogStatus.Published
               orderby blogs.DateCreated descending
               select new
               {
                   blogs.BlogId,
                   blogs.DateCreated,
                   blogs.IsCommentsAllowed,
                   blogs.PostContent,
                   blogs.PostStatus,
                   blogs.PostSummary,
                   blogs.PostTitle,
                   blogs.Tags,
                   blogs.UserId,
                   profiles.BlogName
               };

            var list = (from x in result.AsEnumerable()
                        select new BlogPost
                        {

                            BlogId = x.BlogId,
                            PostTitle = x.PostTitle,
                            PostSummary = x.PostSummary,
                            PostContent = x.PostContent,
                            DateCreated = x.DateCreated,
                            UserId = x.UserId,
                            IsCommentsAllowed = x.IsCommentsAllowed,
                            PostStatus = x.PostStatus.Value,
                            Tags = x.Tags,
                            BlogName = x.BlogName,
                            PostCleanUrl = Utilities.SiteSettings.BaseUrl + "blogs/" + x.BlogName + "/" + x.BlogId + "-" + Imgalib.Utility.StringFormatter.CreateCleanUrl(x.PostTitle)

                        }).Skip(startRowIndex).Take(maximumRows).ToList();

            return list;
            
           
        }

        internal int GetTotalCountForAllActiveSocialShareBlogs(Int32 startRowIndex = default(Int32), int maximumRows = default(Int32))
        {
            var result =
              from
                  blogs in _socialShareDatabaseContext.SocialShare_Blogs
              join
                  profiles in _socialShareDatabaseContext.SocialShare_MemberProfiles
               on
                  blogs.UserId equals profiles.UserId
              where
                  blogs.PostStatus == (Int32)BlogStatus.Published
              select new
              {
                  blogs.BlogId,
                  blogs.DateCreated,
                  blogs.IsCommentsAllowed,
                  blogs.PostContent,
                  blogs.PostStatus,
                  blogs.PostSummary,
                  blogs.PostTitle,
                  blogs.Tags,
                  blogs.UserId,
                  profiles.BlogName
              };
            return result.ToList().Count;
        }

        internal List<BlogPost> GetAllActiveBlogPostsByMember(String userId = default(String))
        {
            var result =
              from
                  blogs in _socialShareDatabaseContext.SocialShare_Blogs
              join
                  profiles in _socialShareDatabaseContext.SocialShare_MemberProfiles
               on
                  blogs.UserId equals profiles.UserId
              where
                  blogs.PostStatus == (Int32)BlogStatus.Published && profiles.UserId == userId
              orderby blogs.DateCreated descending
              select new
              {
                  blogs.BlogId,
                  blogs.DateCreated,
                  blogs.IsCommentsAllowed,
                  blogs.PostContent,
                  blogs.PostStatus,
                  blogs.PostSummary,
                  blogs.PostTitle,
                  blogs.Tags,
                  blogs.UserId,
                  profiles.BlogName
              };

            return result.ToList().ConvertAll(x => new BlogPost
            {
                BlogId = x.BlogId,
                PostTitle = x.PostTitle,
                PostSummary = x.PostSummary,
                PostContent = x.PostContent,
                DateCreated = x.DateCreated,
                UserId = x.UserId,
                IsCommentsAllowed = x.IsCommentsAllowed,
                PostStatus = x.PostStatus.Value,
                Tags = x.Tags,
                BlogName = x.BlogName,
                PostCleanUrl = Utilities.SiteSettings.BaseUrl + "blogs/" + x.BlogName + "/" + x.BlogId + "-" + Imgalib.Utility.StringFormatter.CreateCleanUrl(x.PostTitle)

            });
        }

        internal List<BlogPost> GetAllActiveBlogPostsByMember(Int32 startRowIndex = default(Int32), Int32 maximumRows = default(Int32), String blogName = default(String))
        {
            var result =
              from
                  blogs in _socialShareDatabaseContext.SocialShare_Blogs
              join
                  profiles in _socialShareDatabaseContext.SocialShare_MemberProfiles
               on
                  blogs.UserId equals profiles.UserId
              where
                  blogs.PostStatus == (Int32)BlogStatus.Published && profiles.BlogName == blogName
              orderby blogs.DateCreated descending
              select new
              {
                  blogs.BlogId,
                  blogs.DateCreated,
                  blogs.IsCommentsAllowed,
                  blogs.PostContent,
                  blogs.PostStatus,
                  blogs.PostSummary,
                  blogs.PostTitle,
                  blogs.Tags,
                  blogs.UserId,
                  profiles.BlogName
              };

            return result.ToList().ConvertAll(x => new BlogPost
            {
                BlogId = x.BlogId,
                PostTitle = x.PostTitle,
                PostSummary = x.PostSummary,
                PostContent = x.PostContent,
                DateCreated = x.DateCreated,
                UserId = x.UserId,
                IsCommentsAllowed = x.IsCommentsAllowed,
                PostStatus = x.PostStatus.Value,
                Tags = x.Tags,
                BlogName = x.BlogName,
                PostCleanUrl = Utilities.SiteSettings.BaseUrl + "blogs/" + x.BlogName + "/" + x.BlogId + "-" + Imgalib.Utility.StringFormatter.CreateCleanUrl(x.PostTitle)

            }).Skip(startRowIndex).Take(maximumRows).ToList();
        }

        internal int GetTotalCountActiveBlogPostsByMember(Int32 startRowIndex = default(Int32), int maximumRows = default(Int32), String blogName = default(String))
        {
            var result =
              from
                  blogs in _socialShareDatabaseContext.SocialShare_Blogs
              join
                  profiles in _socialShareDatabaseContext.SocialShare_MemberProfiles
               on
                  blogs.UserId equals profiles.UserId
              where
                  blogs.PostStatus == (Int32)BlogStatus.Published && profiles.BlogName == blogName
              select new
              {
                  blogs.BlogId,
                  blogs.DateCreated,
                  blogs.IsCommentsAllowed,
                  blogs.PostContent,
                  blogs.PostStatus,
                  blogs.PostSummary,
                  blogs.PostTitle,
                  blogs.Tags,
                  blogs.UserId,
                  profiles.BlogName
              };
            return result.ToList().Count;
        }

        internal List<BlogPost> GetAllBlogPosts(String sortOrder = default(String), Int32 startRowIndex = default(Int32), int maximumRows = default(Int32))
        {
            var result =
               from
                   blogs in _socialShareDatabaseContext.SocialShare_Blogs
               join
                   profiles in _socialShareDatabaseContext.SocialShare_MemberProfiles
                on
                   blogs.UserId equals profiles.UserId
               orderby blogs.DateCreated descending
               select new
               {
                   blogs.BlogId,
                   blogs.DateCreated,
                   blogs.IsCommentsAllowed,
                   blogs.PostContent,
                   blogs.PostStatus,
                   blogs.PostSummary,
                   blogs.PostTitle,
                   blogs.Tags,
                   blogs.UserId,
                   profiles.BlogName
               };

            var list = (from x in result.AsEnumerable()
                        select new BlogPost
                        {

                            BlogId = x.BlogId,
                            PostTitle = x.PostTitle,
                            PostSummary = x.PostSummary,
                            PostContent = x.PostContent,
                            DateCreated = x.DateCreated,
                            UserId = x.UserId,
                            IsCommentsAllowed = x.IsCommentsAllowed,
                            PostStatus = x.PostStatus.Value,
                            Tags = x.Tags,
                            BlogName = x.BlogName,
                            PostCleanUrl = Utilities.SiteSettings.BaseUrl + "blogs/" + x.BlogName + "/" + x.BlogId + "-" + Imgalib.Utility.StringFormatter.CreateCleanUrl(x.PostTitle)

                        }).Skip(startRowIndex).Take(maximumRows).ToList();

            return list;


        }

        internal Tuple<Int32, Int32> GetApproveDisapprveTotal()
        {
            Int32 countApprove=_socialShareDatabaseContext.SocialShare_Blogs.Count(n => n.PostStatus.HasValue &&  n.PostStatus.Value== 1);
            Int32 countNonApprove = _socialShareDatabaseContext.SocialShare_Blogs.Count(n => n.PostStatus.Value != 1);
            return new Tuple<int, int>(countApprove, countNonApprove);            
        }

        internal List<CountReport> GetTotalPostsByMonth()
        {
            DateTimeFormatInfo info = new DateTimeFormatInfo();
            List<CountReport> reports = new List<CountReport>();
            var categoryCounts = (from p in _socialShareDatabaseContext.SocialShare_Blogs group p by p.DateCreated.Value.Month into g select new { Category = g.Key, ProductCount = g.Count() });
            for (Int32 index = 0; index < categoryCounts.ToList().Count; index++)
            {
                CountReport report = new CountReport();
                report.Caption = info.GetMonthName(categoryCounts.ToList()[index].Category);
                report.Total = categoryCounts.ToList()[index].ProductCount;
                reports.Add(report);
            }

            return reports;
        }
    }
}
